<?php
class SpendsController extends AppController {

    public $helpers = array('Html', 'Form');
    public $components = array('Session');

    public function index() {
        $pageTitle ="Worldwide Seventhday-Adventisth Church - Kwabrafoso: Expenditure";
        $in_record_data = $this->Spend->find('all');
        $this->set(compact('in_record_data','pageTitle'));
    }

    
    public function add(){
    $pageTitle ="Worldwide Seventh-day Adventist Church - Kwabrafoso: Enter Expenditure";
     if($_POST){
       // pr($_POST);
        $data['Spend'] = array(
            'actionno' => $_POST['actionno'],
            'chqdate' => $_POST['chqdate'],
            'chqno' => $_POST['chqno'],
            'description' => $_POST['description'],
            'total' => $_POST['total'],
            'conference' => $_POST['conference'],
            'district' => $_POST['district'],
            'poorandneedy' => $_POST['poorandneedy'],
            'project' => $_POST['project'],
            'sabsch' => $_POST['sabsch'],
            'persmin' => $_POST['persmin'],
            'chnsabsch' => $_POST['chnsabsch'],
            'secretariat' => $_POST['secretariat'],
            'treasury' => $_POST['treasury'],
            'familylife' => $_POST['familylife'],
            'deacon' => $_POST['deacon'],
            'welfare' => $_POST['welfare'],
            'womensmin' => $_POST['womensmin'],
            'health' => $_POST['health'],
            'advmen' => $_POST['advmen'],
            'funeral' => $_POST['funeral'],
            'communications' => $_POST['communications'],
            'rent' => $_POST['rent'],
            'utilities' => $_POST['utilities'],
            'bankcharges' => $_POST['bankcharges']
        );

        if(!$this->Spend->save($data)){
            $this->Session->setFlash('Error Saving Expenditure');
        }else{
             $this->Session->setFlash('Expenditure Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','errorMsg'));
}
    
    // A functin to delete Expenditure
    public function delete($id = null)
    {
       if (!$id) 
       {
        $this->Session->setFlash('Invalid id for Expenditure');
        $this->redirect(array('action' => 'index'));
    }
    if ($this->Spend->delete($id)) 
    {
        $this->Session->setFlash('Expenditure Deleted Please');
    } else 
    {
        $this->Session->setFlash(__('Expenditure Not Deleted', true));
    }
    $this->redirect(array('action' => 'index'));
}

    // A functin to edit Expenditure
    function edit($id = null){
    $record_id = $id;
    $conditions = array('Spend.id' => $record_id);
    $record = $this->Spend->find('first', array('conditions' => $conditions, 'recursive' => 1));
   // pr($record);
            
     $pageTitle ="Worldwide Seventh-day Adventist Church - Kwabrafoso: Edit Expenditure";
     if($_POST){
        //pr($_POST);
        $data['Spend'] = array(
            'actionno' => $_POST['actionno'],
            'chqdate' => $_POST['chqdate'],
            'chqno' => $_POST['chqno'],
            'description' => $_POST['description'],
            'total' => $_POST['total'],
            'conference' => $_POST['conference'],
            'district' => $_POST['district'],
            'poorandneedy' => $_POST['poorandneedy'],
            'project' => $_POST['project'],
            'sabsch' => $_POST['sabsch'],
            'persmin' => $_POST['persmin'],
            'chnsabsch' => $_POST['chnsabsch'],
            'secretariat' => $_POST['secretariat'],
            'treasury' => $_POST['treasury'],
            'familylife' => $_POST['familylife'],
            'deacon' => $_POST['deacon'],
            'welfare' => $_POST['welfare'],
            'womensmin' => $_POST['womensmin'],
            'health' => $_POST['health'],
            'advmen' => $_POST['advmen'],
            'funeral' => $_POST['funeral'],
            'communications' => $_POST['communications'],
            'rent' => $_POST['rent'],
            'utilities' => $_POST['utilities'],
            'bankcharges' => $_POST['bankcharges']
        );

        if(!$this->Spend->save($data)){
            $this->Session->setFlash('Error Saving Data');
        }else{
             $this->Session->setFlash('Data Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','record'));
}

     // A functin to view Records
    public function view($id) {
    if (!$id) {
        throw new NotFoundException(__('Invalid Record'));
    }

    $spend = $this->Spend->findById($id);
    if (!$spend) {
        throw new NotFoundException(__('Invalid Record'));
    }
    $this->set('spend', $spend);
}

function report($id = null){
    $record_id = $id;
    $conditions = array('Spend.id' => $record_id);
    $record = $this->Spend->find('first', array('conditions' => $conditions, 'recursive' => 1));
//    pr($record);


}


}
?>
